<?php

class CaseAction extends Action 
{
	public function _initialize()
	{
		$this->assign('jumpUrl',__URL__);
 		if(!isset($_SESSION['adminid']))
 		{ 
 			 $url=__APP__.'/Index/login';
 			 redirect($url);return;
 		}		
	}     
    public function index()
    {
		$Case=D("Case");
		$count=$Case->count();
		import("ORG.Util.Page");
		$listRows=10;
		$p=new page($count,$listRows);
		$list=$Case->findAll(null,'*','orderid desc,addtime desc',$p->firstRow.','.$p->listRows);
		$page=$p->show();
		if ($list!==false) {
			$this->assign('page',$page);
			$this->assign('list',$list);			
		}
    				
		$this->display();
    }
    public function add()
    {
    	if(!$this->isPost())
    	{
    		$this->display();
    	}
    	else
    	{
	        $title=isset($_POST['title'])?trim($_POST['title']):'';
	        $link=isset($_POST['link'])?trim($_POST['link']):'';
	        $intro=isset($_POST['intro'])?trim($_POST['intro']):'';
	        $closetime=isset($_POST['closetime'])?trim($_POST['closetime']):'';
	        $orderid=isset($_POST['orderid'])?(int)($_POST['orderid']):0;
	        $htmlbody=isset($_POST['htmlbody'])?trim($_POST['htmlbody']):'';
	        $recomm=isset($_POST['recomm'])?(int)($_POST['recomm']):0;
	        
	        import("ORG.Net.UploadFile");
	        $upload = new UploadFile();     
    	 	$upload->maxSize  = 500*1024 ;   
     		$upload->thumb=false;
     		$upload->autoSub=false;
    	 	$upload->dateFormat="Ymd";
    	 	$upload->saveRule="uniqid";   	    
        	$upload->allowExts  = explode(',','png,jpg,jpeg,gif');
	        $upload->savePath = C('MYUPLOAD_PATH').'/case/'; 
      
        	if(!$upload->upload()) {
	                $this->assign('jumpUrl',__URL__);
    	            $this->error($upload->getErrorMsg());return;            	
        	}
	        $ret=$upload->getUploadFileInfo();
    	    $filename=C('MYUPLOAD_PATH').'/case/'.$ret[0]['savename'];
            $thumb=$filename;
            
            $Case=D("Case");
            if($orderid==0)
            {	
            	$orderid=$Case->max('orderid');
            	$orderid=$orderid+1;
            }            
            $data=array(
            	'title'=>$title,
            	'link'=>$link,
            	'thumb'=>$thumb,            	
            	'intro'=>$intro,
            	'closetime'=>$closetime,
            	'orderid'=>$orderid,
            	'recomm'=>$recomm,
            	'htmlbody'=>$htmlbody,
            );	    		    		
    		if(!$Case->create($data))
    		{
    			$this->error($Case->getError());return;
    		}
    		if(!$Case->add())
    		{ 		
    			$this->error(L('_INSERT_FAIL_'));return;
    		}
    		$this->cache();
    		$this->assign('jumpUrl',__URL__);
    		$msg="案例添加成功";
    		$this->success($msg);
    	}
    }
    public function edit()
    {
    	if(!$this->isPost())
    	{
			$id=isset($_GET['id'])?(int)($_GET['id']):0;
			if (!$id){
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$Case=D("Case");
			$temp=$Case->find($id);
			if (!$temp){
					$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			    					
			$this->assign('vo',$temp);
			$this->display();			    			
    	}
    	else
    	{
			$id=isset($_POST['id'])?(int)($_POST['id']):0;
			$temp=D('Case')->find($id);
			if ($id==0 || !$temp)
			{ 
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}			
			$oldthumb=$temp['thumb'];

	        $title=isset($_POST['title'])?trim($_POST['title']):'';
	        $url=isset($_POST['url'])?trim($_POST['url']):'';
	        $link=isset($_POST['link'])?trim($_POST['link']):'';
	        $intro=isset($_POST['intro'])?trim($_POST['intro']):'';
	        $closetime=isset($_POST['closetime'])?trim($_POST['closetime']):'';
	        $orderid=isset($_POST['orderid'])?(int)($_POST['orderid']):0;	        
	        $htmlbody=isset($_POST['htmlbody'])?trim($_POST['htmlbody']):'';
	        $recomm=isset($_POST['recomm'])?(int)($_POST['recomm']):0;
	        
            $data=array(
            	'id'=>$id,
				'title'=>$title,
				'url'=>$url,
				'link'=>$link,            	
            	'intro'=>$intro,
            	'closetime'=>$closetime,
            	'orderid'=>$orderid,
            	'recomm'=>$recomm,
            	'htmlbody'=>$htmlbody,
            );	 	        
	        			
			if($_FILES['thumb']['name']!='')
			{
	        	import("ORG.Net.UploadFile");
	        	$upload = new UploadFile();     
    	 	    $upload->maxSize  = 500*1024 ;   
     		    $upload->thumb=false;
     		    $upload->autoSub=false;
    	 	    $upload->dateFormat="Ymd";
    	 	    $upload->saveRule="uniqid";     	    
        		$upload->allowExts  = explode(',','png,jpg,jpeg,gif');
	        	$upload->savePath = C('MYUPLOAD_PATH').'/case/'; 
      
        		if(!$upload->upload()) {
	                $this->assign('jumpUrl',__URL__);
    	            $this->error($upload->getErrorMsg());return;            	
        	    }
	            $ret=$upload->getUploadFileInfo();
    	        $filename=C('MYUPLOAD_PATH').'/case/'.$ret[0]['savename'];
    	        if(@file_exists($oldthumb))@unlink($oldthumb);
            	$data['thumb']=$filename;
			}	
			
			$Case=D("Case");
			if(!$Case->create($data))
			{
				$this->error($Case->getError());return;
			}
			if(!$Case->save())
			{
				$this->error(L('_UPDATE_FAIL_'));return;
			}
			$this->cache();			
			$this->assign('jumpUrl',__URL__);
			$msg="案例修改成功";
			$this->success($msg);return;			
    	}    	
    }
	public function del()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		if (!$id){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		$Case=D("Case");
		$temp=$Case->find($id);
		if (!$temp){ $this->error(L('_SELECT_NOT_EXIST_'));return;}		
		if(!$Case->deleteById($id)){
			$this->error(L('_DELETE_FAIL_'));return;
		}
		if(@file_exists($temp['thumb']))@unlink($temp['thumb']);
		$this->cache();
		$this->redirect();
	}	
	private function cache()
	{
		$Case=D("Case");
		$condition['recomm']=1;
		$list=$Case->findAll($condition,'id,title,url,thumb','orderid desc,addtime desc');
		$filename=C('MYCACHE_PATH').'/case.php';
		if(@file_exists($filename)){ @unlink($filename);}
    	$content="<?php\n \$case=".var_export($list,true)."\n?>";
    	@!file_put_contents($filename,$content);	
	}			
}
?>